{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1fee51d2-8d41-468e-b416-a58bed1ddbb9",
   "metadata": {},
   "source": [
    "数据字典地址：http://docs.thinktrader.net/pages/7c0936/"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae164069-0177-4c66-b273-82b21159aae0",
   "metadata": {},
   "source": [
    "若通过客户端下载报错timeout，可在客户端右下角点击【行情】，打开行情源窗口，点击【调度任务】标签页，查看窗口下方“下载信息”处是否有下载客户端下载数据显示。若客户端有正在下载中的任务，可等待客户端下载任务完成后再尝试使用脚本中的函数下载，或中止客户端下载任务后再运行脚本中的下载函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e63c7fc2-2500-4d50-a5af-8af4aaa91c1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Token连接（若通过客户端连接则无需运行此单元格）\n",
    "from vnpy.trader.utility import TEMP_DIR\n",
    "\n",
    "from xtquant import xtdatacenter as xtdc\n",
    "xtdc.set_token(\"xxx\")    # 换成自己的Token\n",
    "xtdc.set_data_home_dir(str(TEMP_DIR) + \"\\\\xt\")\n",
    "xtdc.init()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc8d3d17-28bb-42a9-aea7-95879d9a5994",
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant.xtdata import (\n",
    "    get_full_tick,\n",
    "    get_instrument_detail,\n",
    "    get_local_data,\n",
    "    download_history_data,\n",
    "    get_stock_list_in_sector,\n",
    "    download_history_data,\n",
    "    download_financial_data,\n",
    "    get_financial_data,\n",
    "    get_market_data_ex\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c9ca23f-6978-419d-b679-d077694e0ac6",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "get_full_tick([\"SZO\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "download_history_data(\"\", \"historycontract\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aea61d2-aab4-4189-b19f-862ec22d6a34",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 期权过期合约查询\n",
    "l = get_stock_list_in_sector(\"过期中金所\")\n",
    "# get_stock_list_in_sector(\"过期上证期权\")\n",
    "# get_stock_list_in_sector(\"过期深证期权\")\n",
    "print(len(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f76b380-9d30-4dde-a2b6-913193b49b60",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 获取期权合约信息\n",
    "get_instrument_detail(\"90000001.SZO\", True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a04439d-b14f-48a9-9269-420a0c7c2c58",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 期权数据（1d也支持）\n",
    "download_history_data(\"90000001.SZO\", \"1m\", \"20200101\", \"20200112\")\n",
    "data1 = get_local_data([], [\"90000001.SZO\"], \"1m\", \"20200101\", \"20200112\")\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32f8a0db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 期货数据（1d也支持）\n",
    "download_history_data(\"rb2401.SF\", \"1m\", \"20230601\", \"20231030\")\n",
    "data1 = get_local_data([], [\"rb2401.SF\"], \"1m\", \"20231026\", \"20231030\")\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b900ae1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = data1[\"90000001.SZO\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ade0d1d-1905-4316-b1b4-ebd62cc3834c",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 分笔成交数据\n",
    "download_history_data(\"rb2309.SF\", \"tick\", \"20230821\", \"20230822\")\n",
    "data2 = get_local_data([], [\"rb2309.SF\"], \"tick\", \"20230821\", \"20230822\")\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7269450a-ea0c-4652-9d0d-bd31b572067a",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 财务数据\n",
    "download_financial_data([\"600519.SH\"])\n",
    "data3 = get_financial_data([\"600519.SH\"])\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2dbe8942-b091-4838-b205-67980b45a6ce",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 板块成分股列表\n",
    "get_stock_list_in_sector(\"SZO\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cc3e575-aa47-41d9-ab8d-b1d757f9ba52",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 仓单\n",
    "download_history_data(\"\", \"warehousereceipt\")\n",
    "data4 = get_market_data_ex([], [\"rb.SF\"], period=\"warehousereceipt\")\n",
    "data4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ff13cb1-2793-42e7-9f2f-10f1175d552b",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 期货席位\n",
    "download_history_data(\"rb2401.SF\", \"futureholderrank\", \"20231011\", \"20231012\")\n",
    "data5 = get_market_data_ex([], [\"rb2401.SF\"], \"futureholderrank\")[\"rb2401.SF\"]\n",
    "data5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aba5fac4-527f-4688-a1b8-dd68dbd66484",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 资金流向数据（目前只有股票数据）\n",
    "# transactioncount1m也支持\n",
    "download_history_data(\"000333.SZ\", \"transactioncount1d\")\n",
    "data6 = get_market_data_ex([], [\"000333.SZ\"], period=\"transactioncount1d\")\n",
    "data6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51ec686f-d93f-42a6-9f97-bca9c7c75bdc",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 港股资金流向\n",
    "# northfinancechange1d也支持\n",
    "download_history_data(\"FFFFFF.SGT\", \"northfinancechange1d\")\n",
    "data7 = get_market_data_ex([], [\"FFFFFF.SGT\"], period=\"northfinancechange1d\")\n",
    "data7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "191e22b6-77c8-4976-a0dd-ea6f0337edf2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
